﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.OutboundManage
{
    public partial class LostModify :Common.PageBase
    {

        T_CALLLOSS_BLL bll = new T_CALLLOSS_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            btnCancel.Attributes["onclick"] = UserLoginInfo.FilterBackUrl(btnCancel.Attributes["onclick"]);
            PageHelper.SetPopCalender(this.Page, txtRECORDTIME, "yyyy-MM-dd HH:mm:ss");
            PageHelper.SetPopCalender(this.Page, txtALLOCATEDTIME, "yyyy-MM-dd HH:mm:ss");

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                ddlSTATUSBinding();

                pageDataInit();
            }
        }

        #region 页面初始化数据绑定


        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(LostStatus));
        }



  



        #endregion

        private void pageDataInit()
        {
            string _id = String.IsNullOrEmpty(Request.QueryString["ID"]) ? "" : Request.QueryString["ID"].Trim();

            if (!String.IsNullOrEmpty(_id))
            {
                T_CALLLOSS_Entity entity = bll.GetEntity("ID", _id);

                PageHelper.SetControlValue<T_CALLLOSS_Entity>(this.Page, entity);
                PageHelper.SetControlEnabled(this.Page, false);
                txtALLOCATEDTIME.Text = entity.ALLOCATEDTIME.HasValue ? entity.ALLOCATEDTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtRECORDTIME.Text = entity.RECORDTIME.HasValue ? entity.RECORDTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtREMARK.Enabled = true;

            }

            txtID.ReadOnly = true;

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _id = String.IsNullOrEmpty(Request.QueryString["ID"]) ? "" : Request.QueryString["ID"].Trim();


            T_CALLLOSS_Entity entity = bll.GetEntity("ID", _id);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_CALLLOSS_Entity>(this.Page, ref entity);
                entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                entity.FIRSTCALLBACKTIME = entity.FIRSTCALLBACKTIME.HasValue ? entity.FIRSTCALLBACKTIME.Value : DateTime.Now;
                entity.ALLOCATEDTIME = entity.ALLOCATEDTIME.HasValue ? entity.ALLOCATEDTIME.Value : DateTime.Now;

                Button btn = sender as Button;
                switch (btn.ID)
                {
                    case "btnOver":
                        {
                            if (entity.ASSIGNEDPERSONID.HasValue)
                            {
                                entity.ASSIGNEDPERSONID = _userLoginInfo.UserEntity.USERID;
                                entity.ASSIGNEDPERSONNAME = _userLoginInfo.UserEntity.USERNAME;
                            }
                            else if(entity.ASSIGNEDPERSONID.Value == 0)
                            {
                                entity.ASSIGNEDPERSONID = _userLoginInfo.UserEntity.USERID;
                                entity.ASSIGNEDPERSONNAME = _userLoginInfo.UserEntity.USERNAME;
                            }
                            entity.CALLBACKCOMPLETETIME = DateTime.Now;
                            entity.STATUS = ((int)LostStatus.完成);
                        }
                        break;
                    case "btnSave":
                        {
                            entity.STATUS = ((int)LostStatus.未接通);
                        }
                        break;
                }

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this.Page), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            
        }
    }
}
